﻿using System;

namespace Utils.Extensions
{
	public static class DateTimeExtension
	{
		public static DateTime ChangeDate(this DateTime dateTime, int year, int month, int day)
		{
			return new DateTime(year,
								month,
								day,
								dateTime.Hour,
								dateTime.Minute,
								dateTime.Second,
								dateTime.Millisecond,
								dateTime.Kind);
		}

		public static DateTime ChangeDate(this DateTime dateTime, DateTime newDate)
		{
			return new DateTime(newDate.Year,
								newDate.Month,
								newDate.Day,
								dateTime.Hour,
								dateTime.Minute,
								dateTime.Second,
								dateTime.Millisecond,
								dateTime.Kind);
		}

		public static DateTime ChangeTime(this DateTime dateTime, int hours, int minutes, int seconds, int milliseconds)
		{
			return new DateTime(dateTime.Year,
			                    dateTime.Month,
			                    dateTime.Day,
			                    hours,
			                    minutes,
			                    seconds,
			                    milliseconds,
			                    dateTime.Kind);
		}

		public static DateTime ChangeTime(this DateTime dateTime, TimeSpan newTime)
		{
			return new DateTime(dateTime.Year,
								dateTime.Month,
								dateTime.Day,
								newTime.Hours,
								newTime.Minutes,
								newTime.Seconds,
								newTime.Milliseconds,
								dateTime.Kind);
		}

        public static DateTime RemoveMilliSeconds(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year,
                                dateTime.Month,
                                dateTime.Day,
                                dateTime.Hour,
                                dateTime.Minute,
                                dateTime.Second,
                                0,
                                dateTime.Kind);
        }

        public static bool IsWeekend(this DateTime dateTime)
        {
            return dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday;
        }

        public static DateTime GetPreviousNonWeekendDay(this DateTime dateTime)
        {
            DateTime result = dateTime.Date;

            do
            {
                result = result.AddDays(-1);
            } while (result.DayOfWeek == DayOfWeek.Saturday || result.DayOfWeek == DayOfWeek.Sunday);

            return result;
        }

        public static DateTime GetNextNonWeekendDay(this DateTime dateTime)
        {
            DateTime result = dateTime.Date;

            do
            {
                result = result.AddDays(1);
            } while (result.DayOfWeek == DayOfWeek.Saturday || result.DayOfWeek == DayOfWeek.Sunday);

            return result;
        }

		public static bool IsBetween(this DateTime dateTime, DateTime start, DateTime end)
		{
			return dateTime.CompareTo(start) >= 0 && dateTime.CompareTo(end) <= 0;
		}
		
	}
}
